package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachPlanServiceImpl implements TeachPlanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        //根据课程id查询教学计划树形结构
        List<TeachplanDto> teachplanTree = teachplanMapper.selectTreeNodes(courseId);
        return teachplanTree;
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        //通过课程计划id判断是否新增和修改
        Long teachplanId = saveTeachplanDto.getId();
        if ( teachplanId== null) {
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            //确定排序字段，找到它的同级节点的个数，排序字段就是个数加1
            Long parentId = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            Integer count = getTeachplanCount(courseId, parentId);
            teachplan.setOrderby(count+1);
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            Teachplan teachplan = teachplanMapper.selectById(teachplanId);
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);


        }
    }

    @Override
    public void deleteTeachplan(Long Id) {
        Teachplan teachplan = teachplanMapper.selectById(Id);
        //判断是大章节还是小章节
        if(teachplan.getGrade()==1){
            //大删除
            //有小章节时不允许删除
            if (getTeachplanCount(teachplan.getCourseId(),Id)>0) {
                XueChengPlusException.cast("有小章节不允许删除");
            }
            //没有小章节允许删除
            teachplanMapper.deleteById(Id);
        }else {
            //小删除
            //将关联的视频等一并删除
            teachplanMapper.deleteById(Id);
            //删除相关联的媒资文件
            LambdaQueryWrapper<TeachplanMedia> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(TeachplanMedia::getTeachplanId,Id);
            teachplanMediaMapper.delete(queryWrapper);
        }
    }

    @Override
    public void moveDown(Long id) {
        //课程计划向下移动
        //根据courseId查询课程计划，然后通过parentId判断是小章节还是大章节
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan.getParentid() != 0) {
            Integer orderby = teachplan.getOrderby();
            //证明是小章节
            //根据父章节的id，找出大章节下面所有小节点
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid()).eq(Teachplan::getGrade,2);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            int i = 0;
            for (Teachplan t:
                    teachplans) {
                //找到当前章节的排序字段加一,以此获得下一个章节
                if(t.getOrderby()==orderby+1){
                    i++;
                }
                if(i==1){
                    //进行交换
                    teachplan.setOrderby(t.getOrderby());
                    t.setOrderby(orderby);
                    teachplanMapper.updateById(t);
                    teachplanMapper.updateById(teachplan);
                    break;
                }
            }
            if(i==0){
                XueChengPlusException.cast("当前计划已经在末尾，无法交换");
            }

        } else {
            Integer orderby = teachplan.getOrderby();
            //证明是大章节
            //根据课程的id，找到下一个大章节
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId()).eq(Teachplan::getGrade,1);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            int i = 0;
            for (Teachplan t:
                    teachplans) {
                //找到当前章节的排序字段加一,以此获得下一个章节
                if(t.getOrderby()==orderby+1){
                    i++;
                }
                if(i==1){
                    //进行交换
                    teachplan.setOrderby(t.getOrderby());
                    t.setOrderby(orderby);
                    teachplanMapper.updateById(t);
                    teachplanMapper.updateById(teachplan);
                    break;
                }
            }
            if(i==0){
                XueChengPlusException.cast("当前计划已经在末尾，无法交换");
            }
        }
    }

    @Override
    public void moveUp(Long id) {
        //课程计划向上移动
        //根据courseId查询课程计划，然后通过parentId判断是小章节还是大章节
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan.getParentid() != 0) {
            Integer orderby = teachplan.getOrderby();
            //证明是小章节
            //根据父章节的id，找出大章节下面所有小节点
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid()).eq(Teachplan::getGrade,2);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            int i = 0;
            for (Teachplan t:
                    teachplans) {
                //找到当前章节的排序字段减一,以此获得上一个章节
                if(t.getOrderby()==orderby-1&&(orderby-1)!=0){
                    i++;
                }
                if(i==1){
                    //进行交换
                    teachplan.setOrderby(t.getOrderby());
                    t.setOrderby(orderby);
                    teachplanMapper.updateById(t);
                    teachplanMapper.updateById(teachplan);
                    break;
                }
            }
            if(i==0){
                XueChengPlusException.cast("当前计划已经在顶部，无法交换");
            }

        } else {
            Integer orderby = teachplan.getOrderby();
            //证明是大章节
            //根据课程的id，找到上一个大章节
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId()).eq(Teachplan::getGrade,1);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            int i = 0;
            for (Teachplan t:
                    teachplans) {
                //找到当前章节的排序字段减一,以此获得上一个章节
                if(t.getOrderby()==orderby-1&&(orderby-1)!=0){
                    i++;
                }
                if(i==1){
                    //进行交换
                    teachplan.setOrderby(t.getOrderby());
                    t.setOrderby(orderby);
                    teachplanMapper.updateById(t);
                    teachplanMapper.updateById(teachplan);
                    break;
                }
            }
            if(i==0){
                XueChengPlusException.cast("当前计划已经在顶部，无法交换");
            }
        }
    }

    //教学计划绑定媒资
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //先删除原有的视频
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId()));
        //添加新的视频
        TeachplanMedia teachplanMedia=new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

    //删除关联视频
    @Override
    public void deleteassociationMedia(Long teachplanid, String mediaid) {
        LambdaQueryWrapper<TeachplanMedia> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,teachplanid).eq(TeachplanMedia::getMediaId,mediaid);
        teachplanMediaMapper.delete(queryWrapper);
    }

    private Integer getTeachplanCount(Long courseId, Long parentId) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper=queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }
}

